///////////////////////////////////////////////////////////////////
// Commented by alyaxey because this code used dao.getCodesByExample that is no longer avaivable. 
// One may use dao.getCodePortionByExample and dao.getCodeByCodePortion instead. 
///////////////////////////////////////////////////////////////////

package ua.kpi.ipsa.expert.api.algorithm;

import java.util.*;

import ua.kpi.ipsa.expert.api.db.*;
import ua.kpi.ipsa.expert.api.net.*;

/**
 * 
 * @author doctorpro
 * 
 */
public class SpaceIgnorer implements Algorithm {

	//
	private DAO dao;

	/**
	 * 
	 * @param dao
	 */
	public SpaceIgnorer(DAO dao) {
		this.dao = dao;
	}

	/**
	 * 
	 */
	@Override
	public List<PlagiarismHypoTuple> process(int codeToAnalize,
			CodeRanker ranker, ProgressMonitor monitor) {

		System.out.println("algo running1");

		//
		CodeTuple code = null;
		try {
			code = dao.getCode(codeToAnalize);
			String my = code.text;
			String other = null;

			// get all codes
			CodePortionTuple portionExample = new CodePortionTuple();
			List<CodePortionTuple> codePortions = dao.getCodePortionsByExample(
					portionExample, Long.MAX_VALUE);

			for (Iterator<CodePortionTuple> portionIterator = codePortions
					.iterator(); portionIterator.hasNext();) {
				List<CodeTuple> codesList = dao
						.getCodeByPortionKey(portionIterator.next().codePortionPK);

				for (Iterator<CodeTuple> codeIterator = codesList.iterator(); codeIterator
						.hasNext();) {
					CodeTuple nextCode = codeIterator.next();

					// init other
					other = nextCode.text;

					boolean mismatchFound = false;
					int i = 0;
					int j = 0;
					while (!mismatchFound && (i < my.length())
							&& (j < other.length())) {
						while ((my.charAt(i) == ' ') && (i < my.length())) {
							i++;
						}
						while ((other.charAt(j) == ' ') && (j < other.length())) {
							j++;
						}
						if ((i < my.length()) && (j < other.length())) {
							if (my.charAt(i) != other.charAt(j)) {
								mismatchFound = true;
							}
						}
						i++;
						j++;
					}

					if (!mismatchFound) {
						PlagiarismHypoTuple hpt = new PlagiarismHypoTuple();
						hpt.code1FK = codeToAnalize;
						hpt.code2FK = nextCode.codePK;
					}
				}
			}
		} catch (DataAccessException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 
	 */
	@Override
	public String getName() {
		return "Space Ignorer";
	}

	@Override
	public String[] getCodePresentationClasses() {
		return new String[] {};
	}
}
